package com.pz.cloud.framework.leagueMem.model;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMemFee;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemFeeService;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemService;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.model.BaseImportModel;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

/**
 * 团费导入
 * @author mzz
 */
@Component
public class LeagueMemFeeImportModel extends BaseImportModel<LeagueMemFee> {

    @Autowired
    private ILeagueMemFeeService leagueMemFeeService;

    @Autowired
    private ILeagueMemService leagueMemService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 获取导入模板文件
     * @return
     */
    @Override
    protected  String getTemplateFilePath(){
        return "excelTemplate/import/leagueMemFee.xlsx";
    }

    /**
     * 保存导入的数据
     */
    @Override
    public void saveData(LeagueMemFee data){
        if(StringUtils.isEmpty(data.getStdNo())){
            throw new CustomRuntimeException("学号不能为空！");
        }
        String stdNo = data.getStdNo();
        SysUser user = sysUserService.getUserByName(stdNo);
        if (user == null) {
            throw new CustomRuntimeException("学号" + stdNo + "匹配不到用户");
        }

        LambdaQueryWrapper<LeagueMem> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LeagueMem::getUserId, user.getId());
        LeagueMem leagueMem = leagueMemService.getOne(wrapper);
        if (leagueMem == null) {
            throw new CustomRuntimeException("学号" + stdNo + "匹配不到团员");
        }

        data.setMemId(leagueMem.getId());
        leagueMemFeeService.save(data);
    }


    /**
     * 导入模板名称
     * @return
     */
    @Override
    public String getDemoFileName(){
            return "团费导入模板";
    }

}
